// Created: Dec 13, 2014
//
// Author: wjinwen.1988@gmail.com

#ifndef CHI_NET_ACCEPTOR_H
#define CHI_NET_ACCEPTOR_H 

#include "chi/net/channel.h"
#include "chi/net/socket.h"
#include "chi/net/callbacks.h"

namespace chi {
namespace net {

class EventLoop;
class InetAddress;

// Acceptor of incoming TCP connections.
class Acceptor {
 public:
  using NewConnCallback = std::function<void(int, const InetAddress&)>;

  Acceptor(EventLoop* loop, const InetAddress& listenaddr);
  ~Acceptor();

  // 禁止拷贝对象
  Acceptor(const Acceptor&) = delete;
  void operator=(const Acceptor&) = delete;

  void set_new_conn_cb(const NewConnCallback& cb);

  bool listenning() const { return listenning_; }
  void Listen();
  void StopListen();
 private:
  void HandleRead();

  EventLoop* loop_;
  Socket accept_socket_;
  Channel accept_channel_;
  NewConnCallback new_conn_cb_;
  bool listenning_;
  int idle_fd_;  // 预留的socket，防止socket耗干以后，能正常accept连接。
};

}
}  
#endif 
